<template>
  <div class="container">
    <div>
      <h3>叩丁狼商品管理系统</h3>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm"
        :size="formSize" status-icon>
        <!-- status-icon图标 -->
        <el-form-item prop="name">
          <el-input v-model="ruleForm.name" :prefix-icon="Avatar" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" type="password" :prefix-icon="Lock" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">提交</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import Cookies from "js-cookie"
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { Avatar, Lock } from "@element-plus/icons-vue"
import * as api from "@/request/api"
import { useUserInfoStore } from "@/stores/userInfo";
import { useRouter } from "vue-router"

const router = useRouter();
const formSize = ref('default')
// 0 获取 el-form 的dom————将输入的字符 设置响应式。
// ruleFormRef 与 dom元素上的ref="ruleFormRef"一致
const ruleFormRef = ref<FormInstance>()
const a = ref<FormInstance>();
console.log(a);

console.log(ruleFormRef);

// 表单的字段
const ruleForm = reactive({
  name: '',
  password: '',
})

const rules = reactive<FormRules>({
  // blur 失去焦点触发。 change 输入时触发
  name: [
    { required: true, message: '请填写用户名', trigger: ['blur', 'change'] },
  ]
})


// const submitForm = async (formEl: FormInstance | undefined) => {
//   if (!formEl) return
//   await formEl.validate((valid, fields) => {
//     if (valid) {
//       console.log('submit!')
//     } else {
//       console.log('error submit!', fields)
//     }
//   })
// }


// 1 直接获取 ruleFormRef对象，获取ruleFormRef.value节点，再校验
const submitForm = () => {
  if (ruleFormRef.value) {
    ruleFormRef.value.validate(async (valid, fields) => {

      if (valid) {
        // 拆分了api后，api.adminLogin ---> api.admin.adminLogin()
        // 【1】登录请求
        let result = await api.admin.adminLogin({
          username: ruleForm.name,
          password: ruleForm.password
        });
        // result是Promise<T>的T定义的类型。

        // 设置请求cookie
        Cookies.set('token', result.tokenHead + result.token);

        // 【2】请求用户信息
        let { menus, username, roles } = await api.admin.getAdminInfo();
        useUserInfoStore().updateUserInfo(menus, username, roles);//使用pinia里的方法更新pinia的数据

        // 缓存数据
        // let menus = JSON.stringify(adminInfo.menus);
        // localStorage.setItem('menus', menus);


        // 跳转到首页
        router.push("/index");




      }
    })
  }

}

// 2 在template使用的ruleFormRef 表示的是ruleFormRef.value。直接校验。
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>

<style lang="scss" scoped>
.container {
  background: #2d3a4b;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  h3 {
    padding: 10px 0;
    font-size: 24px;
    color: #fff;
    text-align: center;
    font-weight: bold;
  }

  // 使用‘:deep 第三方组件库的样式名’来更改样式
  :deep .el-input__wrapper {
    border: 1px solid hsla(0, 0%, 100%, .1);
    background-color: rgba(0, 0, 0, .1);
    box-shadow: none;
  }


  :deep .el-form-item__content {
    margin-left: 0 !important;

  }

  .el-input {
    height: 50px;
    width: 418px;

    :deep input {
      color: #fff;
    }
  }

  :deep .el-input__inner {
    padding: 0 15px;
    line-height: 40px;
    font-size: 18px;
  }

  :deep .el-input__prefix-inner {
    color: #454545;
    font-size: 20px;
    margin: 0;
  }

  :deep .el-input__suffix-inner {
    font-size: 20px;
  }

  .el-button {
    width: 100%;
    height: 40px;
  }

}
</style>